package cn.csust.net2.manager.server.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.csust.net2.manager.server.dao.FileLoadNotificationDAO;
import cn.csust.net2.manager.server.dao.FileNotificationDAO;
import cn.csust.net2.manager.server.security.AuthCurrentUser;
import cn.csust.net2.manager.shared.exception.BeyondAuthorityException;
import cn.csust.net2.manager.shared.po.Academy;
import cn.csust.net2.manager.shared.po.FileLoadNotification;
import cn.csust.net2.manager.shared.po.FileNotification;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.po.Role;
import cn.csust.net2.manager.shared.po.Teacher;
import cn.csust.net2.manager.shared.service.FileNoticService;
import cn.csust.net2.manager.shared.util.Constant;
import cn.csust.net2.manager.shared.util.ServiceConstant;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
@Service(value = ServiceConstant.SERVICE_NAME_FILENOTIC)
public class FileNoticServiceImpl extends BaseServiceImpl implements FileNoticService{
	@Resource(name = Constant.DAO_NAME_FILELOADNOTIFICATION)
	private FileLoadNotificationDAO fileLoadNotificationDAO;
	@Resource(name = Constant.DAO_NAME_FILENOTIFICATION)
	private FileNotificationDAO fileNotificationDAO;
	@Transactional
	@Override
	public List<Academy> findAllAcademy() {
		List<Academy> list = fileLoadNotificationDAO.findListByHQL_T(" from Academy ");
		return list;
	}
	public FileLoadNotificationDAO getFileLoadNotificationDAO() {
		return fileLoadNotificationDAO;
	}
	public void setFileLoadNotificationDAO(
			FileLoadNotificationDAO fileLoadNotificationDAO) {
		this.fileLoadNotificationDAO = fileLoadNotificationDAO;
	}
	public FileNotificationDAO getFileNotificationDAO() {
		return fileNotificationDAO;
	}
	public void setFileNotificationDAO(FileNotificationDAO fileNotificationDAO) {
		this.fileNotificationDAO = fileNotificationDAO;
	}
	@Transactional
	@Override
	public Integer saveFileNotification(FileNotification f, List<Integer> ids) throws BeyondAuthorityException {
		if(!isSchool())
			throw new BeyondAuthorityException();
		fileNotificationDAO.save(f);
		for(Integer in:ids){
			
			this.getSqlMapper().addFileNotification(in, f.getFileNotificationID());
			
		}
		return f.getFileNotificationID();
	}
	@Transactional
	@Override
	public String getFileAcademyHtmlInfo(Integer fileNotificationId) {
		// TODO Auto-generated method stub
		
		List<Academy> list = this.getSqlMapper().findAcademy(fileNotificationId);
		FileNotification fnt = fileNotificationDAO.findUniqueHQL(" from FileNotification c where c.fileNotificationID ="+fileNotificationId);
		StringBuffer sb = new StringBuffer("" +
				"<div>需上传部门：</div>" +
				"" +
				"" +
				"" +
				"<table style='border:solid #add9c0; border-width:1px 0px 0px 1px;' >");
		int i =0;
		for(Academy a :list){
			FileLoadNotification fln = fileNotificationDAO.findUniqueHQL_T(" from  FileLoadNotification c where c.fileNotification.fileNotificationID = "+fileNotificationId +" and c.academy.academyID = "+a.getAcademyID());
			
				
				if(i==0)
					sb.append("<tr>");
				if(fln==null){
					
					sb.append("<td style='background-color:#EEEE00;border:solid #add9c0; border-width:0px 1px 1px 0px;' >"+a.getAcademyName()+"</td >");
				
				}else if(fln.getPushDate().after(fnt.getEndDate())){
					
					sb.append("<td style='background-color:#FF0000;border:solid #add9c0; border-width:0px 1px 1px 0px;' ><a href='http://127.0.0.1:8888/manager/download_center.do?type=U&id="+fln.getFileLoadNotificationID()+"' >"+a.getAcademyName()+"</a>  </td  >");
					
				}else if(fln.getPushDate().before(fnt.getEndDate())){
					
					sb.append("<td style='background-color:#00CD00;border:solid #add9c0; border-width:0px 1px 1px 0px;' ><a href='http://127.0.0.1:8888/manager/download_center.do?type=U&id="+fln.getFileLoadNotificationID()+"' >"+a.getAcademyName()+"</a>  </td  >");
				}else{
					
					sb.append("<td style='background-color:#00FFFF;border:solid #add9c0; border-width:0px 1px 1px 0px;' ><a href='http://127.0.0.1:8888/manager/download_center.do?type=U&id="+fln.getFileLoadNotificationID()+"' >"+a.getAcademyName()+"</a>  </td  >");
				}
				
				
				
				if(i==4){
					sb.append("</tr>");
					i=0;
				}
				i++;
				
			
		}
		sb.append("</table >" +
				"" +
				"<div>注：点击既可以下载（<span style='background-color:#EEEE00'>黄色未上传</span>,<span style='background-color:#FF0000'>红色上传过期</span>，<span style='background-color:#00CD00' >绿色按时上传</span>）</div>" +
				"" +
				"");
		
		
		return sb.toString();
	}
	@Transactional
	@Override
	public <T extends PO> BasePagingLoadResult<T> findAll(String cla,
			PagingLoadConfig config) throws BeyondAuthorityException {
		if(cla.contains("FileLoadNotification")){
			Academy a = 	((Teacher)(AuthCurrentUser.get())).getAcademy();
			return fileLoadNotificationDAO.findAllByPaging(config," from FileLoadNotification c where c.academy.academyID = "+a.getAcademyID());
		}else{
			return super.findAll(cla, config);
		}
			
		
	}
	@Transactional
	@Override
	public Integer saveUpLoadFileNotic(String name, String url, String id) {
		FileNotification fnt = fileLoadNotificationDAO.findUniqueHQL_T(" from FileNotification c where c.fileNotificationID ="+id);
		FileLoadNotification fln = new FileLoadNotification();
		fln.setAcademy(((Teacher)(AuthCurrentUser.get())).getAcademy());
		fln.setFileName(name);
		fln.setFileUrl(url);
		fln.setFileNotification(fnt);
		fln.setPushDate(new Date());
		fileLoadNotificationDAO.save(fln);
		return fln.getFileLoadNotificationID();
	}
	@Transactional
	@Override
	public void delete(PO t) throws BeyondAuthorityException {
		if(t instanceof FileLoadNotification){
			fileLoadNotificationDAO.delete(t);
		}else if(t instanceof FileNotification){
			if(!isSchool())
				throw new BeyondAuthorityException();
			FileNotification fnt = (FileNotification)t;
			this.getSqlMapper().deleteFileLoadNotification(fnt.getFileNotificationID());
			fileLoadNotificationDAO.delete(t);
		}
		
	}
	@Transactional
	@Override
	public FileNotification findFileNotificationByID(String id) {
		return fileNotificationDAO.findUniqueHQL("from FileNotification c where c.fileNotificationID = "+id);
	}
	@Transactional
	@Override
	public FileLoadNotification findFileLoadNotificationByID(String id) {
		// TODO Auto-generated method stub
		return fileLoadNotificationDAO.findUniqueHQL(" from FileLoadNotification c where c.fileLoadNotificationID ="+id);
	}
	@Transactional
	@Override
	public boolean isSchool() {
		List<Role> roles=this.getSqlMapper().loadRole(AuthCurrentUser.get().getUserID());
		for(Role r:roles){
			if(r.getRoleName().equals("School"))
				return true;
			else if(r.getRoleName().equals("Teacher"))
				break;
		}
		return false;
	}
	

}
